/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  MalfunctionAdapter.java   
 * @Package com.jiyi.adapter   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-9-4 上午10:28:38   
 * @version V1.0     
 */
package com.jiyi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.activity.new_maintenance.MalfReportActivity;
import com.jiyi.activity.new_maintenance.TempReportReadActivity;
import com.jiyi.bean.maintenance.RepairTask.RepairTaskItem;
import com.swift.maintenance.R;

import java.util.List;

/**
 * @ClassName: MalfunctionAdapter
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-9-4 上午10:28:38
 * 
 */
public class MalfunctionAdapter extends BaseAdapter {
	private List<RepairTaskItem> dataList;
	private Context mContext;
	int parentType,parentPosition;
	
	public MalfunctionAdapter(Context context, List<RepairTaskItem> data,int type ,int position) {
		this.mContext = context;
		this.dataList = data;
		this.parentType = type;
		this.parentPosition = position;
	}

	@Override
	public int getCount() {
		if (dataList != null) {
			return dataList.size();
		} else {
			return 0;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final RepairTaskItem item = dataList.get(position);
		
		ViewHolder viewHolder;
		if (convertView == null) {

			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_malfunction, parent, false);
			viewHolder.tvPosition = (TextView) convertView.findViewById(R.id.tvPosition);
			viewHolder.tvNo = (TextView) convertView.findViewById(R.id.tvNo);
			viewHolder.tvEquip = (TextView) convertView.findViewById(R.id.tvEquip);
			viewHolder.tvFunction = (TextView) convertView.findViewById(R.id.tvFunction);
			viewHolder.llTempReport = (LinearLayout) convertView.findViewById(R.id.llTempReport);
			viewHolder.rlWrite = (RelativeLayout) convertView.findViewById(R.id.rlWrite);
			viewHolder.tvWrite = (TextView) convertView.findViewById(R.id.tvWrite);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.tvPosition.setText(item.getPlace());
		viewHolder.tvNo.setText(item.getId());
		viewHolder.tvEquip.setText(item.getEquipment_name());
		viewHolder.tvFunction.setText(item.getBad_name());

		switch (Integer.parseInt(item.getIs_upload())) {
		case 1:
			//已填写实测记录
			viewHolder.llTempReport.setVisibility(View.VISIBLE);
			viewHolder.rlWrite.setVisibility(View.GONE);
			viewHolder.llTempReport.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(),TempReportReadActivity.class);
					intent.putExtra("bdid", item.getId());
					intent.putExtra("index", (position+1));
					v.getContext().startActivity(intent);
				}
			});
			break;

		default:
			//未填写实测记录
			viewHolder.llTempReport.setVisibility(View.GONE);
			viewHolder.rlWrite.setVisibility(View.VISIBLE);
			viewHolder.tvWrite.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(),MalfReportActivity.class);
					
					intent.putExtra("parentType", parentType);
					intent.putExtra("parentPosition", parentPosition);
					intent.putExtra("childPosition", position);
					intent.putExtra("id", item.getId());
					v.getContext().startActivity(intent);
//					item.setMaintenance_status("1");
//					notifyDataSetChanged();
//					if (parentType == 0) {
//						EventBus.getDefault().post(new RepairTaskListEvent(RepairTaskListEvent.ALL_LIST,parentPosition,dataList,1));
//					}
//					if (parentType == 1) {
//						EventBus.getDefault().post(new RepairTaskListEvent(RepairTaskListEvent.REPAIR_LIST,parentPosition,dataList,1));
//					}
				}
			});
			break;
		}
		
		
		
		return convertView;
	}

	class ViewHolder {

		private TextView tvPosition;
		private TextView tvNo;
		private TextView tvEquip;
		private TextView tvFunction;
		private LinearLayout llTempReport;
		private RelativeLayout rlWrite;
		private TextView tvWrite;
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}
